# -*- coding: utf-8 -*-
# !/usr/bin/env python
import decimal
import json
import datetime
from django.http import HttpResponse


class FormatEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, datetime.date):
            return obj.strftime("%Y-%m-%d")
        elif isinstance(obj, decimal.Decimal):
            return str(obj)
        elif isinstance(obj, bytes):
            return str(obj, encoding='utf-8')
        else:
            return json.JSONEncoder.default(self, obj)


def ValueEncoder(data_list):
    if isinstance(data_list, list):
        for data_list_dict in data_list:
            for key, value in data_list_dict.items():
                if isinstance(value, datetime.datetime):
                    data_list_dict[key] = value.strftime('%Y-%m-%d %H:%M:%S')
                elif isinstance(value, datetime.date):
                    data_list_dict[key] = value.strftime("%Y-%m-%d")
    return data_list


class Result():
    success_code = 1
    fail_code = 0

    @staticmethod
    def success(data=[], msg="查询成功", count=-1):
        result = {"code": Result.success_code, "success": True, "data": data, "msg": msg}
        if count != -1:
            result["count"] = count
        return json.dumps(result, cls=FormatEncoder)

    @staticmethod
    def success_response(data=[], msg="查询成功", count=-1, cookies={}):
        result = Result.success(data, msg, count)
        response = HttpResponse(result, content_type="application/json;charset=utf-8")
        for item in cookies.keys():
            response.set_cookie(item, cookies[item])
        return response

    @staticmethod
    def fail(data=[], code="1", msg=""):
        result = {"code": code, "msg": msg, "success": False, 'data': data}
        return json.dumps(result, cls=FormatEncoder)

    @staticmethod
    def fail_response(data=[], code="1", msg="", status=200):
        result = Result.fail(data, code, msg)
        return HttpResponse(result, content_type="application/json;charset=utf-8", status=status)


class SResult():
    success_code = 1
    fail_code = 0

    @staticmethod
    def success(data=[], msg="查询成功", count=-1):
        data = ValueEncoder(data)
        result = {"code": SResult.success_code, "success": True, "data": data, "msg": msg}
        if count != -1:
            result["count"] = count
        return result

    @staticmethod
    def error(data=[], msg="", count=-1):
        if isinstance(data, list):
            data = ValueEncoder(data)
        result = {"code": SResult.fail_code, "success": False, "data": data, "msg": msg}
        if count != -1:
            result["count"] = count
        return result


class ReturnClass():
    @staticmethod
    def if_ok(tuple_n):
        return tuple_n['status']

    @staticmethod
    def get_value(tuple_n):
        return tuple_n['data']

    @staticmethod
    def return_bust(str):
        return {
            "status": False,
            "data": str
        }

    @staticmethod
    def return_succeed(str):
        return {
            "status": True,
            "data": str
        }
